package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.vo.CountByTimeVo;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CustomerReservationServiceImpl implements CustomerReservationService {

    @Autowired
    private CustomerReservationMapper customerReservationMapper;

    /**
     * 新增预约记录
     *
     * @param reservationDto
     */
    @Override
    public void addReservation(ReservationDto reservationDto) {
        Reservation reservation = new Reservation();
        BeanUtils.copyProperties(reservationDto,reservation);
        customerReservationMapper.addReservation(reservation);
    }

    /**
     * 分页查询预约
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse getByPage(Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page<ReservationVo> page = customerReservationMapper.getByPage(status);
        ReservationVo vo = new ReservationVo();
        PageResponse of = PageResponse.of(page, vo.getClass());
        return of;
    }

    /**
     * 取消预约
     * @param id
     */
    @Override
    public void cancelReservation(Integer id) {
        Reservation vo = new Reservation();

        vo.setId((long)id);
        vo.setStatus(2);
        customerReservationMapper.cancelReservation(vo);
    }

    /**
     * 查询取消预约数量
     *
     * @param userId
     * @return
     */
    @Override
    public Integer getCancelledReservationCount(Long userId) {
        Integer count = customerReservationMapper.getCancelledReservationCount(userId);
        return count;
    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @param time
     * @return
     */
    @Override
    public List<CountByTimeVo> getCountByTime(Long time) {
        //时间戳转化LocalDateTime对象
        Instant instant = Instant.ofEpochMilli(time);
        LocalDateTime date = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
        //调用接口返回数据
        List<CountByTimeVo> list = customerReservationMapper.getCountByTime(date.toString());
        //对获取到的count操作，count值就是一个时间段内有几个预约记录，6-count 就是剩余可用预约数量，为0或null都会显示已约满
        List<CountByTimeVo> collect = list.stream().map(vo -> {
            vo.setCount(6 - vo.getCount());
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    @Scheduled(cron = "0 1,31 * * * *")
    public void myTask() {
        List<Integer> list = customerReservationMapper.getReservationId();
        customerReservationMapper.updateReservation(list);
    }
}
